package com.heima.wemedia.controller.v1;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.heima.model.common.dtos.PageResponseResult;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.wemedia.dtos.SensitiveDto;
import com.heima.model.wemedia.pojos.WmSensitive;
import com.heima.wemedia.service.SensitiveService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;

//敏感词过滤
@RestController
@RequestMapping("/api/v1/sensitive")
public class SensitiveController {
    /**
     * 1 list?page=1&size=10 参数可以不加任何注解 只要参数名和url上参数一致 可以传递文件 图片
     * 或者@RequestParam
     * 2 sensitive/1  /sensivie/{1}  属于url中的一部分 @PathVariable("id")
     * 3  传递是json  @RequestBody
     */
    @Autowired
    private SensitiveService sensitiveService;

    /**
     * 实现分页查询
     *
     * @param dto
     * @return
     */
    @PostMapping("/list")
    public ResponseResult list(@RequestBody SensitiveDto dto) {
        // 效验数据
        dto.checkParam();
        // 执行业务逻辑
        Page<WmSensitive> page = new Page<>(dto.getPage(), dto.getSize());
        LambdaQueryWrapper<WmSensitive> wrapper = new LambdaQueryWrapper<>();
        if (StringUtils.isNotBlank(dto.getName())) {
            wrapper.like(WmSensitive::getSensitives, dto.getName());
        }
        //链式编程简化开发
       /* Wrappers.<WmSensitive>lambdaQuery().
                like(StringUtils.isNotBlank(dto.getName()),
                        WmSensitive::getSensitives,dto.getName());*/
        Page<WmSensitive> resultPage = sensitiveService.page(page, wrapper);
        PageResponseResult result = new PageResponseResult((int) resultPage.getCurrent(),
                (int) resultPage.getSize(),
                (int) resultPage.getTotal());
        result.setData(resultPage.getRecords());
        return result;

    }

    /**
     * 新增
     *
     * @param wmSensitive
     * @return
     */
    @PostMapping("/save")
    public ResponseResult save(@RequestBody WmSensitive wmSensitive) {
        //判断传递数据是否为空 是空的话直接返回 缺少参数
        if (wmSensitive == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE, "无效数据");
        }
        //新建查询
        LambdaQueryWrapper<WmSensitive> wrapper = new LambdaQueryWrapper<>();
        //引用数据库字段  作比较 判断是否存在 如果存在直接返回数据存在
        wrapper.eq(WmSensitive::getSensitives, wmSensitive.getSensitives());
        WmSensitive one = sensitiveService.getOne(wrapper);
        if (one != null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_EXIST, "数据已存在");
        }
        wmSensitive.setCreatedTime(new Date());
        sensitiveService.save(wmSensitive);
        return ResponseResult.okResult(null);
    }

    /**
     * 修改
     *
     * @param wmSensitive
     * @return
     */
    @PostMapping("/update")
    public ResponseResult update(@RequestBody WmSensitive wmSensitive) {
        //判断传递
        if (wmSensitive == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE, "无效数据");
        }
        LambdaQueryWrapper<WmSensitive> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(WmSensitive::getSensitives, wmSensitive.getSensitives());
        WmSensitive one = sensitiveService.getOne(wrapper);
        if (one != null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_EXIST, "数据已存在");
        }
        sensitiveService.updateById(wmSensitive);
        return ResponseResult.okResult(null);
    }

    /**
     * 根据ID删除
     *
     * @param id
     * @return
     */
    @DeleteMapping("/del/{id}")
    public ResponseResult delete(@PathVariable("id") Integer id) {

        if (sensitiveService.getById(id) == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE, "数据不存在");
        }
        sensitiveService.removeById(id);
        return ResponseResult.okResult(null);
    }


}
